package com.stan.core.conf;

import com.stan.core.contants.Constants;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 配置管理器
 * 单例模式
 */
public class ConfManager {

    private ConfManager(){}

    private static ConfManager confManager = new ConfManager();

    public static ConfManager getConfManagerInstance(){
        if(confManager == null){
            confManager = new ConfManager();
        }
        return confManager;
    }


    private static Properties properties = new Properties();

    // 从配置文件读取配置
    static{
        InputStream is = null;
        try {
            is = ConfManager.class.getClassLoader().getResourceAsStream(Constants.CORE_PROPERTIES_FILE_PATH);
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 获得字符串配置
     * @param key
     * @return
     */
    public static String getProperty(String key){
        return properties.getProperty(key);
    }


    /**
     * 获得boolean值
     * @param key
     * @return
     */
    public static Boolean getBoolean(String key){
        Object obj = properties.get(key);
        Boolean result = null;
        if(obj != null && obj instanceof Boolean){
            result = (Boolean) obj;
        }
        return result;
    }

    /**
     * 获得int值
     * @param key
     * @return
     */
    public static Integer getInteger(String key){
        Object obj = properties.get(key);
        Integer result = null;
        if(obj != null && obj instanceof Integer){
            result = (Integer) obj;
        }
        return result;
    }

    /**
     * 获得long值
     * @param key
     * @return
     */
    public static Long getLong(String key){
        Object obj = properties.get(key);
        Long result = null;
        if(obj != null && obj instanceof Long){
            result = (Long) obj;
        }
        return result;
    }


    /**
     * 获得double值
     * @param key
     * @return
     */
    public static Double getDouble(String key){
        Object obj = properties.get(key);
        Double result = null;
        if(obj != null && obj instanceof Double){
            result = (Double) obj;
        }
        return result;
    }

}






